//import localStorage from 'web-localStorage-cache'
import CONSTANT from './constants'
import { constantRoutes } from '@/router'
import { encrypt, decrypt } from '@/utils/jsencrypt'

/**
 * 设置sidebar状态
 * @param {int} status  0 1
 */
export function setSidebarStatus(status) {
  localStorage.setItem(CONSTANT.SIDEBAR_STATUS, status)
}

/**
 * 获取sidebar状态
 */
export function getSidebarStatus() {
  return localStorage.getItem(CONSTANT.SIDEBAR_STATUS)
}

/**
 * 设置token
 * @param {string} token
 */
export function setToken(token) {
  localStorage.setItem(CONSTANT.TOKEN, token, 30)
}

/**
 * 获取token
 */
export function getToken() {
  return localStorage.getItem(CONSTANT.TOKEN)
}

/**
 * 移除token
 */
export function removeToken() {
  localStorage.removeItem(CONSTANT.TOKEN)
}

/**
 * 获取登录用户名
 */
export function getUsername() {
  return localStorage.getItem(CONSTANT.USERNAME)
}

/**
 * 获取登录密码
 */
export function getPassword() {
  const password = localStorage.getItem(CONSTANT.PASSWORD)
  if (password) {
    return decrypt(password)
  }
  return undefined
}

/**
 * 获取记住我
 */
export function getRememberMe() {
  return Boolean(localStorage.getItem(CONSTANT.REMEMBER_ME))
}

/**
 * 获取用户信息
 */
export function getUserInfo() {
  return {
    username: getUsername(),
    password: getPassword(),
    rememberMe: getRememberMe()
  }
}

/**
 * 设置用户信息
 * @param {username: '', password: '', rememberMe: false } userInfo
 */
export function setUserInfo(userInfo) {
  setUsername(userInfo.username)
  setPassword(userInfo.password)
  setRememberMe(userInfo.rememberMe)
}

/**
 * 设置用户信息
 * @param {username: '', password: '', rememberMe: false } userInfo
 */
export function removeUserInfo(userInfo) {
  removeUsername(userInfo.username)
  removePassword(userInfo.password)
  removeRememberMe(userInfo.rememberMe)
}

/**
 * 设置登录用户名
 * @param {string} username
 */
export function setUsername(username) {
  localStorage.setItem(CONSTANT.USERNAME, username, 30)
}

/**
 * 设置登录密码
 * @param {string} password
 */
export function setPassword(password) {
  localStorage.setItem(CONSTANT.PASSWORD, encrypt(password), 30)
}

/**
 * 设置记住我
 * @param {*} rememberMe
 */
export function setRememberMe(rememberMe) {
  localStorage.setItem(CONSTANT.REMEMBER_ME, rememberMe, 30)
}

/**
 * 移除用户名
 */
export function removeUsername() {
  localStorage.removeItem(CONSTANT.USERNAME)
}

/**
 * 移除密码
 */
export function removePassword() {
  localStorage.removeItem(CONSTANT.PASSWORD)
}

/**
 * 移除记住我
 */
export function removeRememberMe() {
  localStorage.removeItem(CONSTANT.REMEMBER_ME)
}

/**
 * 获取路由信息
 */
export function getRouter() {
  let routers = localStorage.getItem(CONSTANT.ROUTER)
  if (routers) {
    return JSON.parse(routers)
  }
  return false
}

/**
 * 设置路由信息
 * @param {*} routers
 */
export function setRouter(routers) {
  routers = constantRoutes.concat(routers)
  localStorage.setItem(CONSTANT.ROUTER, JSON.stringify(routers))
}
